{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Variational Quantum Classifier Feature Map Comparison\n",
    "\n",
    "Both the first-order and second-order expansion feature maps provided by Aqua use $n$ qubits to encode $n$-dim datapoints. However, raw feature vectors can also be directly used in `VQC` circuit constructions, requiring only $log_2(n)$ qubits to encode $n$-dim datapoints. \n",
    "\n",
    "### Experiment\n",
    "Below we compare the classification performance of `VQC` on the [Wine dataset](https://scikit-learn.org/stable/datasets/index.html#wine-dataset) using `RawFeatureVector` and `ZZFeatureMap` feature maps. As you'll see, the former leads to about $90\\%$ accuracy using only $2$ qubits, whereas the latter achieves only around $50\\%$ accuracy, using $4$ qubits and taking $3\\times$ as long. \n",
    "\n",
    "We first prepare the Wine dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler, MinMaxScaler\n",
    "from sklearn.decomposition import PCA\n",
    "from qiskit.ml.datasets import wine"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then set up the experiment as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy\n",
    "\n",
    "from qiskit import BasicAer\n",
    "from qiskit.circuit.library import TwoLocal, ZZFeatureMap\n",
    "from qiskit.aqua import QuantumInstance, aqua_globals\n",
    "from qiskit.aqua.algorithms import VQC\n",
    "from qiskit.aqua.components.optimizers import COBYLA\n",
    "from qiskit.aqua.components.feature_maps import RawFeatureVector\n",
    "from qiskit.aqua.utils import get_feature_dimension\n",
    "\n",
    "feature_dim = 4 # dimension of each data point\n",
    "training_dataset_size = 20\n",
    "testing_dataset_size = 10\n",
    "random_seed = 10598\n",
    "aqua_globals.random_seed = random_seed \n",
    "\n",
    "sample_Total, training_input, test_input, class_labels = wine(\n",
    "    training_size=training_dataset_size,\n",
    "    test_size=testing_dataset_size,\n",
    "    n=feature_dim,\n",
    "    plot_data=False\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try `RawFeatureVector` first:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VQC accuracy with RawFeatureVector:  0.7\n"
     ]
    }
   ],
   "source": [
    "feature_map = RawFeatureVector(feature_dimension=get_feature_dimension(training_input))\n",
    "vqc = VQC(COBYLA(maxiter=200),\n",
    "            feature_map,\n",
    "            TwoLocal(feature_map.num_qubits, ['ry', 'rz'], 'cz', reps=3),\n",
    "            training_input, test_input)\n",
    "result = vqc.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),\n",
    "                                    seed_simulator=aqua_globals.random_seed,\n",
    "                                    seed_transpiler=aqua_globals.random_seed))\n",
    "print(\"VQC accuracy with RawFeatureVector: \", result['testing_accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's try `ZZFeatureMap`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy with ZZFeatureMap:  0.3\n"
     ]
    }
   ],
   "source": [
    "feature_map = ZZFeatureMap(get_feature_dimension(training_input))\n",
    "vqc = VQC(COBYLA(maxiter=200),\n",
    "            feature_map,\n",
    "            TwoLocal(feature_map.num_qubits, ['ry', 'rz'], 'cz', reps=3),\n",
    "            training_input, test_input)\n",
    "result = vqc.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),\n",
    "                                    seed_simulator=aqua_globals.random_seed,\n",
    "                                    seed_transpiler=aqua_globals.random_seed))\n",
    "print(\"Test accuracy with ZZFeatureMap: \", result['testing_accuracy'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
